
#include "ktvInc.h"
#include "ktvWebCommon.h"

#ifdef KTV_WEB_SERVER_SUPPORT

time_t giStartTime;

void  InitWebTime() {
	 giStartTime =  time( NULL ) ;
}

unsigned long GetTime( )
{
	return (unsigned long)( time( NULL ) - giStartTime );
}

int GetLastError( ) { return errno; }


string UTIL_EscapedToString( const string &strEscape )
{
	// according to RFC 2396

	string strString;

	for( unsigned long i = 0; i < strEscape.size( ); )
	{
		if( strEscape[i] == '%' )
		{
			if( i < strEscape.size( ) - 2 )
			{
				char pBuf[4];

				memset( pBuf, 0, sizeof( char ) * 4 );

				pBuf[0] = strEscape[i + 1];
				pBuf[1] = strEscape[i + 2];

				unsigned int c;

				sscanf( pBuf, "%02X", &c );

				strString += c;

				i += 3;
			}
			else
			{
				//UTIL_LogPrint( "error decoding escaped string - possible truncation, halting decode\n" );

				return strString;
			}
		}
		else if( strEscape[i] == '+' )
		{
			strString += ' ';

			i++;
		}
		else
		{
			strString += strEscape[i];

			i++;
		}
	}

	return strString;
}


string UTIL_ToLower( string strUpper )
{
	for( unsigned long i = 0; i < strUpper.size( ); i++ )
		strUpper[i] = tolower( strUpper[i] );

	return strUpper;
}


bool UTIL_CheckFile( const char *szFile )
{
	// check if file exists

	FILE *pFile = NULL;

	if( ( pFile = fopen( szFile, "r" ) ) == NULL )
		return false;

	fclose( pFile );

	return true;
}

void UTIL_MakeFile( const char *szFile, string strContents )
{
	FILE *pFile = NULL;

	if( ( pFile = fopen( szFile, "wb" ) ) == NULL )
	{
		//UTIL_LogPrint( "warning - unable to open %s for writing\n", szFile );

		return;
	}

	fwrite( (void *)strContents.c_str( ), sizeof( char ), strContents.size( ), pFile );
	fclose( pFile );
}

void UTIL_DeleteFile( const char *szFile )
{
	if( unlink( szFile ) == 0 )
	{
	//UTIL_LogPrint( "deleted \"%s\"\n", szFile );
	}
	else
	{
#ifdef WIN32
		//UTIL_LogPrint( "error deleting \"%s\"\n", szFile );
#else
		//UTIL_LogPrint( "error deleting \"%s\" - %s\n", szFile, strerror( errno ) );
#endif
	}
}

void UTIL_MoveFile( const char *szFile, const char *szDest )
{
	if( UTIL_CheckFile( szDest ) ) {
		//UTIL_LogPrint( "error archiving \"%s\" - destination file already exists\n", szDest );
	}
	else {
		UTIL_MakeFile( szDest, UTIL_ReadFile( szFile ) );
	}

	// thanks MrMister

	UTIL_DeleteFile( szFile );
}

string UTIL_ReadFile( const char *szFile )
{
	FILE *pFile = NULL;

	if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
	{
		//UTIL_LogPrint( "warning - unable to open %s for reading\n", szFile );

		return string( );
	}

	fseek( pFile, 0, SEEK_END );
	unsigned long ulFileSize = ftell( pFile );
	fseek( pFile, 0, SEEK_SET );
	char *pData = (char *)malloc( sizeof( char ) * ulFileSize );
	memset( pData, 0, sizeof( char ) * ulFileSize );
	fread( (void *)pData, sizeof( char ), ulFileSize, pFile );
	fclose( pFile );
	string strFile( pData, ulFileSize );
	free( pData );

	return strFile;
}

unsigned long UTIL_SizeFile( const char *szFile )
{
	FILE *pFile = NULL;

	if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
	{
		//UTIL_LogPrint( "warning - unable to open %s for reading\n", szFile );

		return 0;
	}

	fseek( pFile, 0, SEEK_END );
	unsigned long ulFileSize = ftell( pFile );
	fclose( pFile );

	return ulFileSize;
}

#endif // #ifdef KTV_WEB_SERVER_SUPPORT 

